// Saves options 
function save_options() {
  // Retrieve credentials from form
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;

  // Save using Chrome storage
  chrome.storage.sync.set({'username': username, 'password': password}, function() {
    // Notify that we saved.
    document.getElementById("status").innerHTML = "The options were saved successfully and will be used from now on. You may close this tab.";
  });
}

// Restores form to saved values
function restore_options() {
  // Retrieve using Chrome storage
  chrome.storage.sync.get(null, function(o) {
    if (!o.username || !o.password) {
      return;
    }
    // Restore form
    document.getElementById("username").value = o.username;
    document.getElementById("password").value = o.password;
  });
}

// Add listener to save button
document.getElementById("save").addEventListener("click", save_options);
// Restore options
restore_options();